<?php

namespace app\common\model\ES\base;

use Helper\YaconfHelper;
use Elasticsearch\ClientBuilder;

class ESBase
{

    /**
     * @var \EasySwoole\ElasticSearch\Config|null
     */
    public $config = null;
    /**
     * @var \Elasticsearch\Client|null
     */
    public $client = null;
    public $index = null;

//    public $type = null;


    public function __construct()
    {
        $host = YaconfHelper::get('elasticsearch.host');
        $port = YaconfHelper::get('elasticsearch.port');
        $this->client = ClientBuilder::create()->setHosts(["{$host}:{$port}"])->build();
    }

    public function deleteAll()
    {
        $params = [
            'index' => $this->index,
        ];

        $exists = $this->exists();

        if (!empty($exists)) {
            $response = $this->client->indices()->delete($params);
        }
        return true;
    }

    public function exists()
    {
        $params = [
            'index' => $this->index,
            //                'type' => $this->type,
        ];
        $exists = $this->client->indices()->exists($params);
        return $exists;
    }

    public function insert($body_array, $id = '')
    {
        $exists = $this->exists();

        if (empty($exists)) {
            $this->create_index();
        }
        foreach ($body_array as $key => $value) {
            $params = [
                'index' => $this->index,
                //                'type' => $this->type,
                'body' => $value,
            ];
            $response = $this->client->index($params);
        }
        return true;
    }

    protected function create_index()
    {
        // 创建空索引(必须是新索引名称)
        $params = [
            'index' => $this->index,
            'body' => [
                'settings' => [
                    'number_of_shards' => 2,
                    'number_of_replicas' => 3
                ],

            ]
        ];
        $res = $this->client->indices()->create($params);
        $params['body'] = ['properties' => [
            'contents' => [
                'type' => 'text', // 字符串型
                'analyzer' => 'ik_max_word', //ik_max_word 最细粒度拆分 ik_smart最粗粒度拆分
                'search_analyzer' => 'ik_max_word'
            ]
        ]];
        $res = $this->client->indices()->putMapping($params);
        return true;
    }

    public function search($field, $value, $from = 0, $size = 2, $search_type = "match")
    {
        $value = trim($value);
        if (empty($value)) {
            return [];
        }
        $params = [
            "index" => $this->index,
            //            "type" => $this->type,
            'body' => [
                'query' => [
                    $search_type => [
                        $field => $value,
                    ],
                ],
                'highlight' => [
                    "pre_tags" => ["<b><tag1>", "<b><tag1>"],
                    "post_tags" => ["</tag1></b>", "</tag1></b>"],
                    "fields" => [$field => (object)[]],
                ],
                'from' => $from,
                'size' => $size
            ],
        ];

        $result = $this->client->search($params);
        if (!empty($result)) {
            $hits = $result['hits']['hits'];
            $total = $result['hits']['total'];
        }
        return $result;
    }
}
